package com.huiyujinrong.qiangdanbao.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huiyujinrong.qiangdanbao.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 筛选
 */
public class HomeScreeningActivity extends AppCompatActivity{
    @BindView(R.id.toolbar_Back)
    RelativeLayout back;
    @BindView(R.id.toolbar_Title)
    TextView title;

    @BindView(R.id.screening_personal)
    TextView personal;//个人贷款
    @BindView(R.id.screening_enterprise)
    TextView ecterprise;//企业贷款

    private int PERSONAL=1;
    private int ENTERPRISE=2;

    private FragmentPersonal fragmentPersonal;  //个人贷款fragment
    private FragmentEnterprise fragmentEnterprise;  //企业贷款fragment

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screening);
        ButterKnife.bind(this);
        setToolbar();

        inDataFragment();

        defaulltShowFragment();

    }


    private void setToolbar(){
        back.setVisibility(View.VISIBLE);
        title.setText("自定义筛选");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    /**
     * 初始化 fragment
     */
    private void inDataFragment(){
        fragmentPersonal = new FragmentPersonal();
        fragmentEnterprise = new FragmentEnterprise();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.screening_framlayout,fragmentPersonal)
                .add(R.id.screening_framlayout,fragmentEnterprise)
                .hide(fragmentPersonal)
                .hide(fragmentEnterprise)
                .commit();
    }


    //默认显示 个人贷款 fragment
    private void defaulltShowFragment(){
        getSupportFragmentManager()
                .beginTransaction()
                .show(fragmentPersonal)
                .commit();
    }

    //显示fragment
    private void showFragment(Fragment fragment){
        if(fragment!=null&&fragment.isHidden()){
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(fragmentPersonal)
                    .hide(fragmentEnterprise)
                    .show(fragment)
                    .commit();
        }
    }

    /**
     * 根据点击显示不同样式
     * @param showitem
     */
    private void showTextviewStyle(int showitem){
        switch (showitem){
            case 1:
                personal.setBackground(getResources().getDrawable(R.drawable.shape_button));
                personal.setTextColor(getResources().getColor(R.color.baise));
                ecterprise.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                ecterprise.setTextColor(getResources().getColor(R.color.lanse));

                break;
            case 2:
                ecterprise.setBackground(getResources().getDrawable(R.drawable.shape_button));
                ecterprise.setTextColor(getResources().getColor(R.color.baise));
                personal.setBackground(getResources().getDrawable(R.drawable.shape_lan_kuang));
                personal.setTextColor(getResources().getColor(R.color.lanse));

                break;
            default:
                break;
        }
    }


    @OnClick({R.id.screening_personal,R.id.screening_enterprise})
    void textOnClicks(View view){
        switch (view.getId()){
            case R.id.screening_personal:
                //个人贷款
                showFragment(fragmentPersonal);
                showTextviewStyle(PERSONAL);
                break;
            case R.id.screening_enterprise:
                //企业贷款
                showFragment(fragmentEnterprise);
                showTextviewStyle(ENTERPRISE);
                break;
        }
    }
}
